<template>
  <el-container class="home-container">
    <el-header>
      <h3>电商管理系统</h3>
      <el-badge :value="value" :max="99" class="item">
        <el-button size="small" :index="'/orders'" @click="go('/orders')">新订单</el-button>
      </el-badge>
      <el-button class="logout" type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isColllapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isColllapse"
          :collapse-transition="false"
          :default-active="activePath"
          router
        >
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path+''"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/'+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      //左侧菜单
      menulist: [],
      iconsObj: {},
      value: 0,
      //是否折叠
      isColllapse: false,
      //被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.msg()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    msg() {
      var that = this
      var socket = io('http://127.0.0.1:2120')

      var uid = window.sessionStorage.getItem('id')
      if (uid != null) {
      }
      // socket连接后以uid登录
      socket.on('connect', function() {
        socket.emit('login', uid)
      })
      // 后端推送来消息时
      socket.on('new_msg', function(msg) {
        //两种处理方式

        // 1 追加到页面元素中，可以做成页面的弹窗
        $('#msg').append(msg)
        console.log(window.sessionStorage.getItem('id'))
        //2 播放提示应 ：
        if (msg == window.sessionStorage.getItem('id')) {
          that.value++

          var audio = document.getElementById('play') //浏览器支持 audio
          audio.play() //播放提示音
        }
      })
      // 后端推送来在线数据时
      socket.on('update_online_count', function(online_stat) {
        console.log(online_stat)
      })
    },
    //获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('/v1.Auth/select')
      if (res.code !== 200) {
        return this.$message.error(res.msg)
      }

      this.menulist = res.data
      var icon = new Array()
      var id = new Array()
      var iconsObj = new Array()
      for (var i = 0; i < res.data.length; i++) {
        icon[i] = res.data[i].icon
        id[i] = res.data[i].id
      }
      this.getnewarr(id, icon)
    },
    getnewarr(keyArr, valueArr) {
      var obj = {}
      keyArr.map((v, i) => {
        obj[keyArr[i]] = valueArr[i]
      })
      this.iconsObj = obj
      //   console.log(obj)
      //   return obj
    },
    //点击折叠菜单
    toggleCollapse() {
      this.isColllapse = !this.isColllapse
    },
    //保存链接激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    go(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
      this.$router.push('/orders')
    }
  }
}
</script>
<style>
.home-container {
  height: 100%;
}
.el-header,
.el-footer {
  background-color: #333744;
  color: white;
  position: relative;

  letter-spacing: 5px;

  text-align: center;
}
.logout {
  position: absolute;
  top: 17%;
  right: 3%;
}
.el-aside {
  background-color: #333744;

  color: white;
}
.el-aside .el-menu {
  border-right: none;
}

.el-main {
  background-color: #eaedf1;

  /* text-align: center; */
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.item {
  position: absolute;
  top: 21%;
  right: 9%;
  letter-spacing: 0;
}
</style>